package com.app.controller.backstage;

import com.app.common.Result;
import com.app.mapper.ActivityEnlistMapper;
import com.app.model.ActivityEnlistModel;
import com.app.model.ActivityModel;
import com.app.model.AdminUserModel;
import com.app.service.ActivityAdminService;
import com.app.service.ActivityService;
import com.app.service.AdminUserService;
import com.app.util.TokenUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;

@Controller
@RestController
@RequestMapping("/admin/activity")
public class ActivityAdminController {
    @Autowired
    private ActivityAdminService activityAdminService;
    @Autowired
    private ActivityService activityService;
    @Autowired
    private ActivityEnlistMapper activityEnlistMapper;
    @Autowired
    private AdminUserService adminUserService;

    //分类列表
    @GetMapping("/selectTypeList")
    public Result<Object> selectTypeList() {
        return Result.success(activityService.selectTypeList());
    }

    //增加和编辑
    @PostMapping("/insertAndUpdateActivity")
    public Result<Object> insertAndUpdateActivity(@RequestBody ActivityModel activity, HttpServletRequest request) {
        String token = request.getHeader("authori-zation");
        String userId = TokenUtils.nothingness(token);
        if (userId == null) {
            return Result.error(405, "token不存在");
        }

        AdminUserModel user = adminUserService.getUserById(Integer.parseInt(userId));
        if (user == null) {
            return Result.error(405, "用户不存在");
        }
        if (user.getVillageId() != 0) {
            activity.setVillageId(user.getVillageId());
        } else {
            activity.setVillageId(0);
        }
        if (activity.getId() == 0) {
            activityAdminService.insert(activity);
        } else {
            activityAdminService.updateById(activity);
        }
        return Result.success("操作成功");
    }

    //更新状态
    @GetMapping("/updateStatus")
    public Result<Object> updateStatus(Integer id, Integer status) {
        activityAdminService.updateStatus(id, status);
        return Result.success("更新成功");
    }

    //删除
    @GetMapping("/deleteActivityById")
    public Result<Object> deleteActivityById(Integer id) {
        if (id != null && id != 0) {
            activityAdminService.deleteActivityById(id);
        }
        return Result.success("删除成功");
    }

    //查询活动列表
    @GetMapping("/selectActivityList")
    public Result<Object> selectActivityList(Integer villageId, Integer page, Integer limit, String title, HttpServletRequest request) throws ParseException {
        String token = request.getHeader("authori-zation");
        String userId = TokenUtils.nothingness(token);
        if (userId == null) {
            return Result.error(405, "token不存在");
        }

        AdminUserModel user = adminUserService.getUserById(Integer.parseInt(userId));
        if (user == null) {
            return Result.error(405, "用户不存在");
        }
        if (user.getVillageId() == 0 && villageId != null) {
            user.setVillageId(villageId);
        }
        return Result.success(activityAdminService.selectActivityList(page, limit, title, user.getVillageId()));
    }

    //查询活动标题
    @GetMapping("/selectApplicationTitle")
    public Result<Object> selectApplicationTitle(HttpServletRequest request) {
        String token = request.getHeader("authori-zation");
        String userId = TokenUtils.nothingness(token);
        if (userId == null) {
            return Result.error(405, "token不存在");
        }

        AdminUserModel user = adminUserService.getUserById(Integer.parseInt(userId));
        if (user == null) {
            return Result.error(405, "用户不存在");
        }
        return Result.success(activityAdminService.selectApplicationTitle(user.getVillageId()));
    }

    //报名信息查询
    @GetMapping("/selectApplication")
    public Result<Object> selectApplication(Integer page, Integer limit, String keyword, Integer activityId, HttpServletRequest request) {
        String token = request.getHeader("authori-zation");
        String userId = TokenUtils.nothingness(token);
        if (userId == null) {
            return Result.error(405, "token不存在");
        }
        AdminUserModel user = adminUserService.getUserById(Integer.parseInt(userId));
        if (user == null) {
            return Result.error(405, "用户不存在");
        }
        return Result.success(activityAdminService.selectApplication(page, limit, keyword, activityId, user.getVillageId()));
    }

    //审核
    @PostMapping("/examine")
    public Result<Object> examine(@RequestBody ActivityEnlistModel model) {
        ActivityEnlistModel model1 = activityEnlistMapper.selectById(model.getId());
        if (model1.getAuditStatus() != 0) {
            if (model1.getAuditStatus() == 1) {
                return Result.error(405, "已审核，请勿重复审核");
            }
        }
        return Result.success(activityAdminService.examine(model));
    }

    //图片审核
    @GetMapping("/ImageExamine")
    public Result<Object> ImageExamine(Integer auditStatus, Integer id, String auditRemark) {
        activityAdminService.ImageExamine(auditStatus, id, auditRemark);
        return Result.success("");
    }
}
